/*
 * @(#)MaintOptionsSetupForm.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.setup.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.Utility;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractActionForm;
import com.dcivision.framework.web.AbstractListAction;
import com.dcivision.setup.SetupOptionFactory;
import com.dcivision.setup.bean.SetupOption;
import com.dcivision.setup.dao.AbstractSetupOptionDAObject;

/**
 *  ListAction Handler
 *
 *  @author           Kennedy Lam
 *  @company          DCIVision Limited
 *  @creation date    01/08/2003
 *  @version          $Revision: 1.4.26.1 $
 */
public class ListOptionsSetupAction extends AbstractListAction {

  public static final String REVISION = "$Revision: 1.4.26.1 $";


  /**
   * Default Constructor
   */
  public ListOptionsSetupAction() {
    super();
    this.setListName("setupOptionList");
    //log.debug("DEBUG - ListOptionsSetupAction constructor");
  }

  /**
   * Override AbstractListAction.execute()
   * 
   * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
   */
  
  public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws ServletException {
    
    //log.debug("DEBUG - ListOptionsSetupAction.execute()");
    
    AbstractActionForm actionForm = (AbstractActionForm) form;
    
    actionForm.reset(mapping, request);
    actionForm.setOpMode(null);   // force it to reload the page
    actionForm.setNavMode(null);  // after each form action

    StringBuffer sbLog = new StringBuffer();
    sbLog.append(this.getClass().getName());
    sbLog.append(", OP Mode:");
    sbLog.append(actionForm.getOpMode());
    sbLog.append(", Nav Mode:");
    sbLog.append(actionForm.getNavMode());
    log.info(sbLog.toString());
    
    ActionForward forward = this.retrieveFunctionCode(request, response, mapping);  if (forward!=null) {return forward;}
    
    this.getListData(mapping, actionForm, request, response );
    
    return mapping.findForward(GlobalConstant.NAV_MODE_LIST);
  }

  /* (non-Javadoc)
   * @see com.dcivision.framework.web.AbstractAction#getFunctionCode()
   */
  
  public String getFunctionCode() {
    return SystemFunctionConstant.SETUP_OPTION_SETUP;
  }

  /**
   * Irrelevant overridden method.
   * Use getMajorDAObject(HttpServletRequest) instead.
   * 
   * @see com.dcivision.framework.web.AbstractAction#getMajorDAOClassName()
   * @see com.dcivision.setup.web.ListOptionsSetupAction#getMajorDAObject(HttpServletRequest)
   */
  
  public String getMajorDAOClassName() {
    return "com.dcivision.setup.dao.AbstractSetupOptionDAObject";
  }

  

  /* (non-Javadoc)
   * @see com.dcivision.framework.web.AbstractListAction#deleteListData(org.apache.struts.action.ActionMapping, com.dcivision.framework.web.AbstractSearchForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
   */
/*   
  public void deleteListData(ActionMapping mapping, AbstractSearchForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws ApplicationException {

    super.deleteListData(mapping, form, request, response);
  }
*/
  /**
   * Overridden method
   * 
   * @see com.dcivision.framework.web.AbstractListAction#getListData(org.apache.struts.action.ActionMapping, com.dcivision.framework.web.AbstractSearchForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
   */
  public void getListData( ActionMapping mapping, AbstractActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws ApplicationException {

    //log.debug("DEBUG - ListOptionsSetupAction.getListData()");
      
    String recordStatus = "A";
    String status = "*";

    AbstractDAObject obj = getMajorDAObject(request);

    try {
      AbstractSetupOptionDAObject setupOptionDAO = (AbstractSetupOptionDAObject) obj;
      List list = setupOptionDAO.getListByRecordStatusAndStatus(recordStatus, status);
      //remove shortcut Relationship type
      if("SetupOptionDocumentRelationship".equals(request.getParameter("optionsSetupName")) && !Utility.isEmpty(list) ){
        for(int i =0; i < list.size() ;i++){
          SetupOption setupOption = (SetupOption)list.get(i);
          if("L".equals( setupOption.getOptionCode())){
            list.remove(i);
            break;
          }
        }        
      }
      
      request.getSession().setAttribute(this.getListName(), new ArrayList(list));      
    } catch (ApplicationException appEx) {
      throw appEx;
    } catch (Exception e) {
      log.error(e, e);
      throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
    }
  }

  /**
   * Overridden method.
   * Return DAO from request parameter.
   * 
   * @see com.dcivision.framework.web.AbstractAction#getMajorDAObject(javax.servlet.http.HttpServletRequest)
   */
  
  protected AbstractDAObject getMajorDAObject(HttpServletRequest request)
    throws ApplicationException {

    //log.debug("DEBUG - ListOptionsSetupAction.getMajorDAObject()");
    //log.debug("optionsSetupName: " + request.getParameter("optionsSetupName"));

    String setupOptionDAOName = SetupOptionFactory.getDAObjectName(request.getParameter("optionsSetupName"));
    try {
      AbstractSetupOptionDAObject setupOptionDAO = 
        (AbstractSetupOptionDAObject) Class.forName(setupOptionDAOName).newInstance();
      setupOptionDAO.setDbConn(this.getConnection(request));
      setupOptionDAO.setSessionContainer(this.getSessionContainer(request));
      
      //log.debug("setupOptionDAOName: " + setupOptionDAOName);
      
      return setupOptionDAO;
    } catch (Exception e) {
      log.error(e, e);
      throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
    }    

  }

}
